/**
 * 消息提示条
 * @param {object} params
 * @param {number} params.duration 持续时间（毫秒），默认`3000`
 * @param {number} params.zindex 起始定位层级，默认`1000`
 */
function usemessage(params = {}) {
  // console.log('2222222')
  const doc = document
  const cssmodule = `__${Math.random().toString(36).slice(2, 7)}`
  // console.log(cssmodule,'------------------')
  const classname = {
    box: `msg-box${cssmodule}`,
    hide: `hide${cssmodule}`,
    text: `msg-text${cssmodule}`,
    icon: `msg-icon${cssmodule}`
  }
  console.log(classname, 'classs')
  const style = doc.createElement('style')
  style.textContent = `
  .${classname.box}, .${classname.icon}, .${classname.text} {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
  }
  .${classname.box} {
    position: fixed;
    top: 0;
    left: 50%;
    display: flex;
    padding: 12px 16px;
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 3px 3px -2px rgba(0,0,0,.2),0 3px 4px 0 rgba(0,0,0,.14),0 1px 8px 0 rgba(0,0,0,.12);
    white-space: nowrap;
    animation: ${classname.box}-move .4s;
    transition: .4s all;
    transform: translate3d(-50%, 0%, 0);
    opacity: 1;
    overflow: hidden;
  }
  .${classname.box}::after {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 4px;
  }
  @keyframes ${classname.box}-move {
    0% {
      opacity: 0;
      transform: translate3d(-50%, -100%, 0);
    }
    100% {
      opacity: 1;
      transform: translate3d(-50%, 0%, 0);
    }
  }
  .${classname.box}.${classname.hide} {
    opacity: 0;
    /* transform: translate3d(-50%, -100%, 0); */
    transform: translate3d(-50%, -100%, 0) scale(0);
  }
  .${classname.icon} {
    display: inline-block;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    overflow: hidden;
    margin-right: 6px;
    position: relative;
  }
  .${classname.text} {
    font-size: 14px;
    line-height: 18px;
    color: #555;
  }
  .${classname.icon}::after,
  .${classname.icon}::before {
    position: absolute;
    content: "";
    background-color: #fff;
  }
  .${classname.box}.info .${classname.icon}, .${classname.box}.info::after {
    background-color: #1890ff;
  }
  .${classname.box}.success .${classname.icon}, .${classname.box}.success::after {
    background-color: #52c41a;
  }
  .${classname.box}.warning .${classname.icon}, .${classname.box}.warning::after {
    background-color: #faad14;
  }
  .${classname.box}.error .${classname.icon}, .${classname.box}.error::after {
    background-color: #ff4d4f;
  }
  .${classname.box}.info .${classname.icon}::after,
  .${classname.box}.warning .${classname.icon}::after {
    top: 15%;
    left: 50%;
    margin-left: -1px;
    width: 2px;
    height: 2px;
    border-radius: 50%;
  }
  .${classname.box}.info .${classname.icon}::before,
  .${classname.box}.warning .${classname.icon}::before {
    top: calc(15% + 4px);
    left: 50%;
    margin-left: -1px;
    width: 2px;
    height: 40%;
  }
  .${classname.box}.error .${classname.icon}::after, 
  .${classname.box}.error .${classname.icon}::before {
    top: 20%;
    left: 50%;
    width: 2px;
    height: 60%;
    margin-left: -1px;
    border-radius: 1px;
  }
  .${classname.box}.error .${classname.icon}::after {
    transform: rotate(-45deg);
  }
  .${classname.box}.error .${classname.icon}::before {
    transform: rotate(45deg);
  }
  .${classname.box}.success .${classname.icon}::after {
    box-sizing: content-box;
    background-color: transparent;
    border: 2px solid #fff;
    border-left: 0;
    border-top: 0;
    height: 50%;
    left: 35%;
    top: 13%;
    transform: rotate(45deg);
    width: 20%;
    transform-origin: center;
  }
  `.replace(/(\n|\t|\s)*/ig, '$1').replace(/\n|\t|\s(\{|\}|\,|\:|\;)/ig, '$1').replace(/(\{|\}|\,|\:|\;)\s/ig, '$1')
  doc.head.appendChild(style)
  /** 一直累加的定位层级 */
  let zindex = params.zindex || 1000
  /**
   * 消息队列
   * @type {array<htmlelement>}
   */
  const messagelist = []

  /**
   * 获取指定`item`的定位`top`
   * @param {htmlelement=} el
   */
  function getitemtop(el) {
    let top = 10
    for(let i = 0; i < messagelist.length; i++) {
      const item = messagelist[i]
      if(el && el === item) {
        break
      }
      top += item.clientHeight + 20
    }
    return top
  }

  /**
   * 删除指定列表项
   * @param {htmlelement} el
   */
  function removeitem(el) {
    for(let i = 0; i < messagelist.length; i++) {
      const item = messagelist[i]
      if(item === el) {
        messagelist.splice(i, 1)
        break
      }
    }
    el.classList.add(classname.hide)
    messagelist.forEach(function(item) {
      item.style.top = `${getitemtop(item)}px`
    })
  }

  /**
   * 显示一条消息
   * @param {string} content 内容
   * @param {"info"|"success"|"warning"|"error"} type 消息类型
   * @param {string} duration 持续时间，优先级比默认值高
   */
  function show(content, type = 'info', duration) {
    const el = doc.createElement('div')
    el.className = `${classname.box} ${type}`
    el.style.position = 'absolute'
    // el.style.top = `${getitemtop()}px`;
    el.style.top = `50px`
    el.style.left = `50%`
    el.style.zindex = zindex
    // el.style.backgroundColor = '#f2f4f5';
    // el.style.minWidth = '160px';
    // el.style.maxWidth = '240px';
    // el.style.minHeight = '40px';
    // el.style.maxHeight = '60px';
    // el.style.borderRadius = '15px';
    // el.style.padding = '10px';
    // el.style.transition = 'all 1s'
    el.innerHTML = `
    <span class="${classname.icon}"></span>
    <span class="${classname.text}">${content}</span>
    `
    zindex++
    messagelist.push(el)
    doc.body.appendChild(el)
    setTimeout(() => {
      doc.body.removeChild(el)
    }, 1500)
    // 添加动画监听事件
    // function animationend() {
    //   el.removeEventListener("animationend", animationend);
    //   setTimeout(removeitem, duration || params.duration || 3000, el);
    // }
    // el.addEventListener("animationend", animationend);
    // function transitionend() {
    //   if (getComputedStyle(el).opacity !== "0") return;
    //   el.removeEventListener("transitionend", transitionend);
    //   el.remove();
    // }
    // el.addEventListener("transitionend", transitionend);
  }

  return {
    show,
    /**
     * 普通描述提示
     * @param {string} msg
     */
    info(msg) {
      show(msg, 'info')
    },
    /**
     * 成功提示
     * @param {string} msg
     */
    success(msg) {
      show(msg, 'success')
    },
    /**
     * 警告提示
     * @param {string} msg
     */
    warning(msg) {
      show(msg, 'warning')
    },
    /**
     * 错误提示
     * @param {string} msg
     */
    error(msg) {
      show(msg, 'error')
    }
  }
}
